package cn.zwz.bus.controller;

import cn.zwz.basics.baseVo.PageVo;
import cn.zwz.basics.baseVo.Result;
import cn.zwz.basics.utils.PageUtil;
import cn.zwz.basics.utils.ResultUtil;
import cn.zwz.bus.entity.BusinessType;
import cn.zwz.bus.service.IBusinessTypeService;
import cn.zwz.data.utils.ZwzNullUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 郑为中
 * CSDN: Designer 小郑
 */
@Slf4j
@RestController
@Api(tags = "业务类型管理接口")
@RequestMapping("/zwz/businessType")
@Transactional
public class BusinessTypeController {

    @Autowired
    private IBusinessTypeService iBusinessTypeService;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条业务类型")
    public Result<BusinessType> get(@RequestParam String id){
        return new ResultUtil<BusinessType>().setData(iBusinessTypeService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部业务类型个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iBusinessTypeService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部业务类型")
    public Result<List<BusinessType>> getAll(){
        return new ResultUtil<List<BusinessType>>().setData(iBusinessTypeService.list());
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询业务类型")
    public Result<IPage<BusinessType>> getByPage(@ModelAttribute BusinessType businessType ,@ModelAttribute PageVo page){
        QueryWrapper<BusinessType> qw = new QueryWrapper<>();
        if(!ZwzNullUtils.isNull(businessType.getTitle())) {
            qw.like("title",businessType.getTitle());
        }
        if(!ZwzNullUtils.isNull(businessType.getContent())) {
            qw.like("content",businessType.getContent());
        }
        IPage<BusinessType> data = iBusinessTypeService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<BusinessType>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改业务类型")
    public Result<BusinessType> saveOrUpdate(BusinessType businessType){
        if(iBusinessTypeService.saveOrUpdate(businessType)){
            return new ResultUtil<BusinessType>().setData(businessType);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增业务类型")
    public Result<BusinessType> insert(BusinessType businessType){
        iBusinessTypeService.saveOrUpdate(businessType);
        return new ResultUtil<BusinessType>().setData(businessType);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑业务类型")
    public Result<BusinessType> update(BusinessType businessType){
        iBusinessTypeService.saveOrUpdate(businessType);
        return new ResultUtil<BusinessType>().setData(businessType);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除业务类型")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iBusinessTypeService.removeById(id);
        }
        return ResultUtil.success();
    }
}
